package com.api.qfengx.data.hrm;

import com.alibaba.fastjson.JSON;
import com.weaver.qfengx.DaoUtils;
import com.weaver.qfengx.StringUtils;
import com.weaver.qfengx.config.DevModeConfig;
import com.weaver.qfengx.entity.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author Qfengx <qffg1998@126.com>
 * @Date 2021-08-05 14:51:01
 */
@Path("/qfengx/data/hrm")
public class DataHrmApi {


    @POST
    @Path("/dept/id")
    @Produces(MediaType.APPLICATION_JSON)
    public String hrmDeptId(@Context HttpServletRequest request,
                            @Context HttpServletResponse response) {
        try {
            String outkey = request.getParameter("outkey");
//            String fixedToken = request.getParameter("fixed_token");
            if (StringUtils.isEmpty(outkey)) {
                return JSON.toJSONString(Result.paramErr());
            }
//            if (!StringUtils.equals(fixedToken, DevModeConfig.getPropConfig("api.fixed.token", "f808f256e8df4dfc9f1f90633b194a13").getValue())) {
//                return JSON.toJSONString(Result.tokenErr());
//            }
            Map<String, String> resMap = DaoUtils.executeQueryToMap("select id, departmentname , outkey, departmentcode from hrmdepartment where outkey = ?", outkey);
            if (resMap == null || resMap.isEmpty()) {
                return JSON.toJSONString(Result.fail("部门不存在"));
            }
            return JSON.toJSONString(Result.ok(resMap));
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(Result.exception().setMsg(e.getMessage()));
        }
    }

    @POST
    @Path("/subcom/id")
    @Produces(MediaType.APPLICATION_JSON)
    public String hrmSubcomId(@Context HttpServletRequest request,
                              @Context HttpServletResponse response) {
        try {
            String outkey = request.getParameter("outkey");
//            String fixedToken = request.getParameter("fixed_token");
            if (StringUtils.isEmpty(outkey)) {
                return JSON.toJSONString(Result.paramErr());
            }
//            if (!StringUtils.equals(fixedToken, DevModeConfig.getPropConfig("api.fixed.token", "f808f256e8df4dfc9f1f90633b194a13").getValue())) {
//                return JSON.toJSONString(Result.tokenErr());
//            }
            Map<String, String> resMap = DaoUtils.executeQueryToMap("select id, subcompanycode, subcompanyname, outkey from hrmsubcompany where outkey = ?", outkey);
            if (resMap == null || resMap.isEmpty()) {
                return JSON.toJSONString(Result.fail("分部不存在"));
            }
            return JSON.toJSONString(Result.ok(resMap));
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(Result.exception().setMsg(e.getMessage()));
        }
    }


    @POST
    @Path("/user/id")
    @Produces(MediaType.APPLICATION_JSON)
    public String hrmUserId(@Context HttpServletRequest request,
                            @Context HttpServletResponse response) {
        try {
            String loginid = request.getParameter("loginid");
//            String fixedToken = request.getParameter("fixed_token");
            if (StringUtils.isEmpty(loginid)) {
                return JSON.toJSONString(Result.paramErr());
            }
//            if (!StringUtils.equals(fixedToken, DevModeConfig.getPropConfig("api.fixed.token", "f808f256e8df4dfc9f1f90633b194a13").getValue())) {
//                return JSON.toJSONString(Result.tokenErr());
//            }
            // 获取人员ID
            Map<String, String> resMap = DaoUtils.executeQueryToMap("select id, lastname as name, workcode, loginid from hrmresource where loginid = ?", loginid);
            if (resMap == null || resMap.isEmpty()) {
                return JSON.toJSONString(Result.fail("人员不存在"));
            }
            return JSON.toJSONString(Result.ok(resMap));
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(Result.exception().setMsg(e.getMessage()));
        }
    }

    @GET
    @Path("/user/info/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public String hrmUserId(@PathParam("id") String id, @Context HttpServletRequest request,
                            @Context HttpServletResponse response) {
        try {
            if (StringUtils.isEmpty(id)) {
                return JSON.toJSONString(Result.paramErr());
            }
            String sql = "select loginid, email, lastname, hrmresource.id, sex, departmentid, hrmresource.subcompanyid1,status ,hrmsubcompany.subcompanyname,hrmdepartment.departmentname " +
                    "from hrmresource " +
                    "left join hrmsubcompany on hrmsubcompany.id = hrmresource.subcompanyid1 " +
                    "left join hrmdepartment on hrmdepartment.id = departmentid where hrmresource.id = ?";
            Map<String, String> map = DaoUtils.executeQueryToMap(sql, id);
            if (map == null || map.isEmpty()) {
                return JSON.toJSONString(Result.fail("用户不存在"));
            }
            // 根据人员ID获取人员信息
            return JSON.toJSONString(Result.ok(map));
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(Result.exception().setMsg(e.getMessage()));
        }
    }

    // 部门下所有人员
    @GET
    @Path("/dept/userlist/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public String hrmDeptId(@PathParam("id") String id, @Context HttpServletRequest request,
                            @Context HttpServletResponse response) {
        try {
            if (StringUtils.isEmpty(id)) {
                return JSON.toJSONString(Result.paramErr());
            }
            List<Map<String, String>> mapList = DaoUtils.executeQueryToMapList("select loginid, email, lastname, hrmresource.id, sex, " +
                    "departmentid, hrmresource.subcompanyid1,status ,hrmsubcompany.subcompanyname,hrmdepartment.departmentname,workcode " +
                    "from hrmresource " +
                    "left join hrmsubcompany on hrmsubcompany.id = hrmresource.subcompanyid1 " +
                    "left join hrmdepartment on hrmdepartment.id = departmentid " +
                    "where hrmdepartment.id = ?", id);
            return JSON.toJSONString(Result.ok(mapList));
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(Result.exception().setMsg(e.getMessage()));
        }
    }

}
